﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SceneCreator : BaseGame {


	public Vector3 originPos = new Vector3 (235, 0, 0);

	public GameObject obTemplet = null;

	public GameObject heikiz = null;

	public GameObject jumpButton = null;



	private static SceneCreator sceneCreator = null;
	public static SceneCreator GetInstance()
	{
		return sceneCreator;
	}


	public override void Init( string id, string lessonID = "")
	{
		gameCfg = DataManager.gameCfgData.GetCfg (id);
		if (gameCfg == null)
			return;
        nowGame = this;
        music.clip = null;

        lessonId = lessonID;

        hasBegin = false;

		// 关闭游戏2
		SceneCreator2.GetInstance().CloseGame();
		// 打开游戏1
		jumpButton.SetActive (true);

		gameObject.SetActive (true);

		// heikiz
		heikiz.SetActive(true);

		isVideoFinished = false;
        //		PlayVideoFirst (gameCfg.urlVideo);

        gameVideoUI.StartVideo(gameCfg.urlVideo);

        if (gameUI.GetInstance() != null)
            gameUI.GetInstance().RefreshScore(0,0);


        //		ReInit ();
    }
		
	bool preparedReady = false;
	public override void ReInit( bool mansu = true)
	{
		nowGame = this;

        

		Clear ();

		score = 0;

		// 地图初始位置
		transform.localPosition = originPos;

		GameObject lastone = null;

        
		   bool firstgudian = true;
        if (lessonId != "1" && lessonId != "2")
            firstgudian = false;

            gameUI.GetInstance ().HideReward ();
		gameUI.GetInstance ().ShowGuide (false);

		hasBegin = false;
		preparedReady = false;

        cf = mansu ? gameCfg.mansu : gameCfg.kuaisu;

		foreach (var item in cf.gudian) {

			GameObject go = Instantiate (obTemplet) as GameObject;
			go.SetActive (true);
			go.name = "balled";
			go.transform.parent = obTemplet.transform.parent;
			go.transform.localScale = obTemplet.transform.localScale;
			go.transform.localRotation = obTemplet.transform.localRotation;
			go.transform.localPosition = obTemplet.transform.localPosition + new Vector3 (item.Key * cf.speed, 0, 0);

			if (firstgudian) {
				GameObject obs = go.transform.GetChild (1).gameObject;
				obs.tag = "firstGudian";
				firstgudian = false;
			}

			lastone = go;
		}
		GameObject obst = lastone.transform.GetChild (1).gameObject;
		obst.tag = "lastGudian";


//        if (music.clip == null)
        //		if (music.clip != null)
        //			preparedReady = true;
        //		else 
        {

            if (!string.IsNullOrEmpty(cf.urlMusic))
            {
                Loading.GetInstance().StartLoading();
                ResLoader.Load(cf.urlMusic, null, (www) =>
                {
                    AudioClip ac = null;
                    if (string.IsNullOrEmpty(www.error))
                        ac = WWWAudioExtensions.GetAudioClip(www);

                    music.clip = ac;
                    Loading.GetInstance().EndLoading();

                    //					preparedReady = true;
					Invoke("DelayPrepareReady", 0.5f);
                }, true, true);
            }
        }
//        else
//            preparedReady = true;

    }

	public void DelayPrepareReady()
	{
		preparedReady = true;
	}

    public override void OnBtnStartGame()
    {
        preparedReady = true;
    }

    public override void BeginGame()
	{

		if(gameUI.GetInstance() != null )
			gameUI.GetInstance ().RefreshScore (0, cf.gudian.Count);

		
		music.Stop ();
		music.Play ();

		hasBegin = true;
		preparedReady = false;

	}

	void OnDisable()
	{
		music.Stop ();
	}

	void Awake()
	{
		sceneCreator = this;
	}


	void FixedUpdate()
	{
		if (preparedReady && isVideoFinished)
			BeginGame ();

		if (!hasBegin)
			return;


		float deltaDistance = Time.fixedDeltaTime* cf.speed;
		transform.position = transform.position - new Vector3 (deltaDistance, 0, 0);

//		Debug.Log (transform.position + "          time:" + music.time);


	}
		

	public override void Replay()
	{
		Time.timeScale = 1;

		ReInit ( nowMansu );
//		BeginGame ();
	}

	public override void Clear()
	{
		// 消除西瓜
		for (int i = 1; i < transform.childCount; i++) {

			Transform t = transform.GetChild (i);
			Destroy (t.gameObject);
		}

		Resources.UnloadUnusedAssets ();
	}


	public override void CloseGame()
	{
		Clear ();

		music.clip = null;

		jumpButton.SetActive (false);

		gameObject.SetActive (false);



		// heikiz
		heikiz.SetActive(false);
	}




}
